<?php

// Smarty.class.php
require_once('include/sessionclass.php');
session_start();
require_once('libs/Smarty.class.php');
require_once('include/oracle.php');
require_once('include/validation_class.php');
require_once('include/config_color.php');
require_once('include/ngonngu.php');
require_once('include/login.php');

//$USERID = '00000';
$smarty = new Smarty();
/********/
$TCVN=array('¸','µ','¶','·','¹','¨','¾','»','¼','½','Æ', '©','Ê','Ç','È','É','Ë', '®','Ð','Ì','Î','Ï','Ñ', 'ª','Õ','Ò','Ó','Ô','Ö', 
'Ý','×','Ø','Ü','Þ', 'ã','ß','á','â','ä', '«','è','å','æ','ç','é', '¬','í','ê','ë','ì','î', 'ó','ï','ñ','ò','ô', '­','ø','õ','ö','÷','ù', 
'ý','ú','û','ü','þ', '¡','¢','§','£','¤','¥','¦');

/********/
$smarty->template_dir = 'smarty/templates';
$smarty->compile_dir = 'smarty/templates_c';
$smarty->cache_dir = 'smarty/cache';
$smarty->config_dir = 'smarty/configs';
/********English*******/
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$javascript ='
<link href="style/stylefixedheader.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/javascript" src="js/checkall.js"></script>
<script language="JavaScript" type="text/javascript" src="js/tongquat.js"></script>
<META HTTP-EQUIV="Refresh" CONTENT="' . $GLOBALS['conf_time']*60 .';URL=http://' . $host . $uri . '/blank.php?log=logout">
';
$smarty->assign('javascript', $javascript);

$smarty->assign('lbl_levelid', $VN_user[POSID]);
$smarty->assign('lbl_midid', $VN_user[MIDID]);
$smarty->assign('lbl_menuid', $VN_user[MENUID]);
$smarty->assign('lbl_vngiaodich', $VN_user[VN_giaodich]);

$smarty->assign('ten_thuc_hien', $VN_template[ACTION]);
$smarty->assign('ten_themmoi', $VN_template[ADDNEW]);
$smarty->assign('ten_baotri', $VN_template[MAINTAIN]);
$smarty->assign('ten_trove', $VN_template[BACK]);

$smarty->assign('colorlevelid', $succ_color);
$smarty->assign('colormidid', $succ_color);
/**************************************************************************/
$getlevelid = $_GET['levelid'];
$getmidid = $_GET['midid'];

$name = "pro_getMidAC_detail";		

//$resultgd = get_procedure_1($name);
$listgd = array();

$query = "begin $name(:LEVELID,:v_ocur); end;";
$conn = openconnection();
$v_ocur = oci_new_cursor($conn);
$stmt = oci_parse($conn,$query) or die ('Can not parse query');		
oci_bind_by_name($stmt,":LEVELID",$getlevelid,strlen($getlevelid));	
oci_bind_by_name($stmt,":v_ocur",$v_ocur,-1,OCI_B_CURSOR);  

oci_execute($stmt);			
oci_execute($v_ocur);					
oci_close($conn);
$resultgd = $v_ocur;				
while($row = oci_fetch_array ($resultgd,(OCI_NUM+OCI_RETURN_NULLS))){	
	$listgd[$row[0]] = $row[1];	
}
$smarty->assign('listgd', $listgd);
if(isset($_REQUEST['MIDID'])){
	$smarty->assign('activeMIDID', $_REQUEST['MIDID']);
}


if(isset($_POST['findGD'])){	
	$MDLNAME= "AC14242_AccessrighDetailInq";
		
	$LEVELID = $_REQUEST['LEVELID'];
	$MIDID = $_REQUEST['MIDID'];	
	
	$smarty->assign('LEVELID', $LEVELID);
	$smarty->assign('MIDID', $MIDID);
	$smarty->assign('activeMIDID', $MIDID);
	
	//Lay danh sach giao dich da ton tai
	if(empty($MIDID)){
		$error=$VN_accessright[MIDID];
	}
	if(empty($LEVELID)){
		$error=$VN_accessright[LEVELID];
	}	
	if(!empty($LEVELID)&&!empty($MIDID)){
		$strquery = "$USERID|$LEVELID|$MIDID|*EOF|";
		$result = get_procedure_3($MDLNAME,$strquery);		
	}		
	
	$key = 1;	
						
	$dataaccessright ='';
	$grid='';
	$dataaccessright = '			
			<table class="tborder" width="100%" border="0" cellpadding="5" cellspacing="1">
								<thead><tr>
								<th width="5%"></th>								
								<th width="15%">'.$VN_user[MENUID].'</th>
								<th>' .$VN_user[VN_giaodich] .'</th>
								</tr></thead><tbody>';	
																
		 if($row = oci_fetch_array ($result,OCI_NUM + OCI_RETURN_NULLS)){ //tim thay ket qua
			$accessright = new csession_accessright();
			$accessright->LEVELID=$row[0];
			$accessright->MIDID=$row[1];		
			$MENUIDtmp=$row[2];			
			$accessright->MENUID[$key]=$row[2];
			$accessright->VNL[$key]=str_replace($TCVN,$UNICODE,$row[3]);
			
			$grid = $grid . '<tr>';
			$grid = $grid . '<td class="even" align="center"> <input type="checkbox" name="checkgrp[]" value="$row[2]" checked="checked" disabled="disabled" >';
			$j=0;
			foreach($row as $col_value){					
				if($j>1){	
					if(fmod($j,2)==0){
						$grid = $grid . '<td class="odd"';
					}else{
						$grid = $grid . '<td class="even"';
					}
					if($j!=3){
						$grid = $grid . ' align="center">' . $col_value . '</td>';
					}else{
						$grid = $grid . ' align="left">' . str_replace($TCVN,$UNICODE,$col_value) . '</td>';
	        		}				
				}
				$j++;
			}			
					
			while($row = oci_fetch_array ($result,OCI_NUM + OCI_RETURN_NULLS)){		
			//echo "ssssssssssssssssssssssssssssssssssssssssssssssssssssssss";		
			 	$key++;
				$accessright->MENUID[$key]=$row[2];
			    $accessright->VNL[$key]=str_replace($TCVN,$UNICODE,$row[3]);
				//echo "$key ".$row[2];
				$j = 0;
				$grid = $grid . '<tr>';
				$grid = $grid . '<td class="even" align="center"> <input type="checkbox" name="checkgrp[]" value="$row[2]" checked="checked" disabled="disabled" >';
				foreach($row as $col_value){					
					if($j>1){	
						if(fmod($j,2)==0){
							$grid = $grid . '<td class="odd"';
						}else{
							$grid = $grid . '<td class="even"';
						}
						if($j!=3){
							$grid = $grid . ' align="center">' . $col_value . '</td>';
						}else{
							$grid = $grid . ' align="left">' . str_replace($TCVN,$UNICODE,$col_value) . '</td>';
						}						
					}
					$j++;
				}
				$grid = $grid . '</tr>';			 
			}								
										
			if(isset($_SESSION['accessright'])){
				unset($_SESSION['accessright']);
			}
			$_SESSION['accessright'] = serialize($accessright);
				
			$a = new csession_accessright();
			$a = unserialize($_SESSION['accessright']);
			$smarty->assign('LEVELID',$a->LEVELID);
			$smarty->assign('MIDID',$a->MIDID);//activeMIDID			
			$smarty->assign('MENUID',$a->MENUID);
			$smarty->assign('VNL',$a->VNL);
				
		}else{//ko tim thay ket qua
			$error = $VN_template[FAILD];
		}
		
		$dataaccessright = $dataaccessright .  $grid;
		$dataaccessright = $dataaccessright . '</tbody></table>';
		$smarty->assign('dataaccessright', $dataaccessright);
}

/**********************/
if(isset($_POST['action'])){
	$action = $_REQUEST['hanhdong'];
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$LEVELID = $_REQUEST['LEVELID'];
	$MIDID = $_REQUEST['MIDID'];
	
	$smarty->assign('LEVELID', $LEVELID);
	$smarty->assign('MIDID', $MIDID);
	$smarty->assign('activeMIDID', $MIDID);
	
	if(empty($LEVELID)||empty($MIDID)){
		$levelid = "";
		$midid = "";
	}else{
		$levelid = $LEVELID;
		$midid = $MIDID;
	}
	switch ($action){
		case '1'://trang xem danh sach
			redirect("http://$host$uri/accessright1.php");
			//header("Location: http://$host$uri/position.php");
			break;
		case '2'://tai khoan position can duyet
			redirect("http://$host$uri/accessright1_can_duyet.php");
			//header("Location: http://$host$uri/position_can_duyet.php");			
			break;
		case '3':
			redirect("http://$host$uri/them_moi_accessright1.php");
			//header("Location: http://$host$uri/them_moi_position.php");
			break;
		case '4':
			redirect("http://$host$uri/chi_tiet_accessright1.php?levelid=$levelid&midid=$midid");
			//header("Location: http://$host$uri/chi_tiet_position.php?id=$id");
			break;
		case '5':
			redirect("http://$host$uri/bao_tri_accessright1.php?levelid=$levelid&midid=$midid");
			//header("Location: http://$host$uri/bao_tri_position.php?id=$id");
			break;
		case '6':
			redirect("http://$host$uri/duyet_accessright1.php?levelid=$levelid&midid=$midid");
			//header("Location: http://$host$uri/duyet_position.php?id=$id");
			break;
	}
}
if(isset($_POST['addnew'])){
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
			
	redirect("http://$host$uri/them_moi_accessright1.php");
}

if(isset($_POST['maintain'])){
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$LEVELID = $_REQUEST['LEVELID'];
	$MIDID = $_REQUEST['MIDID'];
	
	//echo "LEVELID: $LEVELID; MIDID: $MIDID";
	if(empty($LEVELID)||empty($MIDID)){
		$levelid = "";
		$midid = "";
	}else{
		$levelid = $LEVELID;
		$midid = $MIDID;
	}
	//echo "http://$host$uri/bao_tri_accessright1.php?levelid=$levelid&midid=$midid";
	redirect("http://$host$uri/bao_tri_accessright1.php?levelid=$levelid&midid=$midid");
}

if(!empty($getlevelid)&&!empty($getmidid)&&!isset($_POST['findGD'])){	
	$LEVELID = $getlevelid;
	$MIDID = $getmidid;	
	$smarty->assign('LEVELID', $LEVELID);
	$smarty->assign('MIDID', $MIDID);
	$smarty->assign('activeMIDID', $MIDID);
	$MDLNAME= "AC14242_AccessrighDetailInq";
	$strquery_fun = "$USERID|$MDLNAME|*EOF|";
	
	$p_msg = get_function(fun_getUserRights,$strquery_fun);
	/*NO : ko du quyen
	OK: co quyen thuc hien
	CM003: pro chua duoc dang ky trong bang par_sstxnlist*/
	$result =  $p_msg;
if($result=="NO"){
	$error=$VN_Right[NO];
}elseif($result=="CM003"){
	$error=$VN_Right[ERR];
}else{				
		$LEVELID = $_REQUEST['levelid'];
		$MIDID = $_REQUEST['midid'];
			
		$strquery = "$USERID|$LEVELID|$MIDID|*EOF|";
		//echo 'query: '.$strquery;
		$result = get_procedure_3($MDLNAME,$strquery);
		$key = 1;	
						
		$dataaccessright ='';
		$grid='';
		$dataaccessright = '			
			<table class="tborder" width="100%" border="0" cellpadding="5" cellspacing="1">
								<thead><tr>
								<th width="5%"></th>								
								<th width="15%">'.$VN_user[MENUID].'</th>
								<th>' .$VN_user[VN_giaodich] .'</th>
								</tr></thead><tbody>';	
																
		 if($row = oci_fetch_array ($result,OCI_NUM + OCI_RETURN_NULLS)){ //tim thay ket qua
			$accessright = new csession_accessright();
			$accessright->LEVELID=$row[0];
			$accessright->MIDID=$row[1];		
			$MENUIDtmp=$row[2];			
			$accessright->MENUID[$key]=$row[2];
			$accessright->VNL[$key]=str_replace($TCVN,$UNICODE,$row[3]);
			
			$grid = $grid . '<tr>';
			$grid = $grid . '<td class="even" align="center"> <input type="checkbox" name="checkgrp[]" value="$row[2]" checked="checked" disabled="disabled" >';
			$j=0;
			foreach($row as $col_value){					
				if($j>1){	
					if(fmod($j,2)==0){
						$grid = $grid . '<td class="odd"';
					}else{
						$grid = $grid . '<td class="even"';
					}
					if($j!=3){
						$grid = $grid . ' align="center">' . $col_value . '</td>';
					}else{
						$grid = $grid . ' align="left">' . str_replace($TCVN,$UNICODE,$col_value) . '</td>';
	        		}				
				}
				$j++;
			}			
					
			while($row = oci_fetch_array ($result,OCI_NUM + OCI_RETURN_NULLS)){				
			 	$key++;
				$accessright->MENUID[$key]=$row[2];
			    $accessright->VNL[$key]=str_replace($TCVN,$UNICODE,$row[3]);
				//echo "$key ".$row[2];
				$j = 0;
				$grid = $grid . '<tr>';
				$grid = $grid . '<td class="even" align="center"> <input type="checkbox" name="checkgrp[]" value="$row[2]" checked="checked" disabled="disabled" >';
				foreach($row as $col_value){					
					if($j>1){	
						if(fmod($j,2)==0){
							$grid = $grid . '<td class="odd"';
						}else{
							$grid = $grid . '<td class="even"';
						}
						if($j!=3){
							$grid = $grid . ' align="center">' . $col_value . '</td>';
						}else{
							$grid = $grid . ' align="left">' . str_replace($TCVN,$UNICODE,$col_value) . '</td>';
						}						
					}
					$j++;
				}
				$grid = $grid . '</tr>';			 
			}								
										
			if(isset($_SESSION['accessright'])){
				unset($_SESSION['accessright']);
			}
			$_SESSION['accessright'] = serialize($accessright);
				
			$a = new csession_accessright();
			$a = unserialize($_SESSION['accessright']);
			$smarty->assign('LEVELID',$a->LEVELID);
			$smarty->assign('MIDID',$a->MIDID);//activeMIDID			
			$smarty->assign('MENUID',$a->MENUID);
			$smarty->assign('VNL',$a->VNL);
				
		}else{//ko tim thay ket qua
			$error = $VN_template[FAILD];
		}
		
		$dataaccessright = $dataaccessright .  $grid;
		$dataaccessright = $dataaccessright . '</tbody></table>';
		$smarty->assign('dataaccessright', $dataaccessright);
	}
}


$dorc = array(D => $VNdorc[D] ,C => $VNdorc[C],H => $VNdorc[H]);

$smarty->assign('dorc', $dorc);
$chuyentrang = array(1 => $cbo_chuyentrang[1], 2 => $cbo_chuyentrang[2], 3 => $cbo_chuyentrang[3], 4 => $cbo_chuyentrang[4], 5 => $cbo_chuyentrang[5], 6 => $cbo_chuyentrang[6]);
$smarty->assign('select_cbo_chuyentrang', 4);
$smarty->assign('chuyentrang', $chuyentrang);

//echo 'id='.$_GET['id'].' - find='.$_POST['find'].' - levelid='.$_REQUEST['LEVELID'].' - midid='.$_REQUEST['MIDID'];
//echo $_POST['action'];

/********/
if(isset($error))
	$smarty->assign('error', $error);
else
	$smarty->assign('error', "");

$smarty->display('chi_tiet_accessright1.tpl');

?>